import { _decorator, AnimationClip, Component, Animation, SpriteFrame } from 'cc';
import { FSM_PARAMS_TYPE_ENUM } from '../Enums';
import State from './State';
import { SubStateMachine } from './SubStateMachine';


const { ccclass, property } = _decorator;

type ParamsValueaType = number | boolean

export interface IParamsValue {
    type: FSM_PARAMS_TYPE_ENUM
    value: ParamsValueaType
}

export const getInitParamsTrigger = () => {
    return {
        type: FSM_PARAMS_TYPE_ENUM.TRIGGER,
        value: false
    }

}

export const getInitParamsNumber = () => {
    return {
        type: FSM_PARAMS_TYPE_ENUM.NUMBER,
        value: 0
    }
}

@ccclass('StateMachine')
export abstract class StateMachine extends Component {

    private _currentState: State | SubStateMachine = null

    /**参数列表 */
    params: Map<string, IParamsValue> = new Map()

    /**状态机列表 */
    stateMachine: Map<string, State | SubStateMachine> = new Map()

    playerAnimation: Animation
    waitingList: Array<Promise<SpriteFrame[]>> = []

    getParams(paramsName: string) {
        if (this.params.has(paramsName)) {
            return this.params.get(paramsName).value
        }
    }

    setParams(paramsName: string, value: ParamsValueaType) {
        if (this.params.has(paramsName)) {
            this.params.get(paramsName).value = value
            this.run()
            this.resetTrigger()
        }
    }

    get currentState() {
        return this._currentState
    }
    set currentState(state: State | SubStateMachine) {
        this._currentState = state
        this._currentState.run()
    }

    abstract init(): void 

    abstract run(): void

    resetTrigger() {
        for (const [_, value] of this.params) {
            if (value.type === FSM_PARAMS_TYPE_ENUM.TRIGGER) {
                value.value = false
            }
        }
    }


}


